﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member.UserControl
{
    public partial class ThongTinCaNhan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = DateTime.Now.Year; i > 1891; i--)
                {
                    dlNam.Items.Add(i.ToString());
                }
            }
            if (IsPostBack == false)
            {
                string maTaiKhoan = Request.QueryString["profile"].ToString().Trim();

                TaiKhoanBUS taiKhoanBus = new TaiKhoanBUS();
                ThongTinTaiKhoanBUS thongTinTaiKhoanBus = new ThongTinTaiKhoanBUS();

                TaiKhoanDTO taiKhoanDto = TaiKhoanBUS.GetTaiKhoanOfMa(maTaiKhoan);

                ThongTinTaiKhoanDTO tinTaiKhoanDto = thongTinTaiKhoanBus.GetTaiKhoanOfMa(maTaiKhoan);

                lblTenTK.Text = taiKhoanDto.TenTaiKhoan;

                DateTime dateTime = DateTime.Parse(tinTaiKhoanDto.NgaySinh);
                dlNgay.Text = dateTime.Day.ToString("00");
                dlThang.Text = dateTime.Month.ToString("00");
                dlNam.Text = dateTime.Year.ToString("0000");

                dlGioiTinh.Text = tinTaiKhoanDto.GioiTinh.ToString();

                txtTen.Text = tinTaiKhoanDto.HoTen;
                txtDiaChi.Text = tinTaiKhoanDto.DiaChi;
                txtSoDienThoai.Text = tinTaiKhoanDto.SoDienThoai;
                txtThe.Text = tinTaiKhoanDto.MaTheTinDung;
                email.Text = tinTaiKhoanDto.Email;

            }
            pnlThanhCong.Visible = false;
            pnlLoi.Visible = false;
        }

        protected void btnCapNhap_Click(object sender, EventArgs e)
        {

            string maTaiKhoan = Request.QueryString["profile"].ToString().Trim();

            TaiKhoanBUS taiKhoanBus = new TaiKhoanBUS();
            ThongTinTaiKhoanBUS thongTinTaiKhoanBus = new ThongTinTaiKhoanBUS();
            TaiKhoanDTO taiKhoanDto = new TaiKhoanDTO();
            ThongTinTaiKhoanDTO ttTK = new ThongTinTaiKhoanDTO();


            ttTK.MaTaiKhoan = maTaiKhoan;
            ttTK.HoTen = txtTen.Text.Trim();
            ttTK.DiaChi = txtDiaChi.Text.Trim();
            ttTK.MaTheTinDung = txtThe.Text.Trim();
            ttTK.SoDienThoai = txtSoDienThoai.Text.Trim();

            if (dlGioiTinh.Text.Trim().Equals("Nam"))
            {
                ttTK.GioiTinh = "Nam";
            }
            if (dlGioiTinh.Text.Trim().Equals("Nữ"))
            {
                ttTK.GioiTinh = "Nữ";
            }

            int ngay = int.Parse(dlNgay.Items[dlNgay.SelectedIndex].Value);
            int thang = int.Parse(dlThang.Items[dlThang.SelectedIndex].Value);
            int nam = int.Parse(dlNam.Items[dlNam.SelectedIndex].Value);

            DateTime dateTime = new DateTime(nam, thang, ngay);
            ttTK.NgaySinh = dateTime.ToString();

            bool res = thongTinTaiKhoanBus.UpdateThongTinTaiKhoan(ttTK);

            taiKhoanDto.MaTaiKhoan = maTaiKhoan;
            HttpPostedFile postFile = FileUpload1.PostedFile;
            if (postFile.FileName != "")
            {
                if (!File.Exists(Server.MapPath("~\\images") + "\\" + Path.GetFileName(postFile.FileName)))
                {
                    postFile.SaveAs(Server.MapPath("~\\images") + "\\" + Path.GetFileName(postFile.FileName));
                }

                taiKhoanDto.Avatar = "~\\images" + "\\" + Path.GetFileName(postFile.FileName);
                bool result = taiKhoanBus.UpdateTaiKhoanOfAvatar(taiKhoanDto);
            }
            if (res)
            {
                pnlThanhCong.Visible = true;
                pnlLoi.Visible = false;
            }
            else
            {
                pnlThanhCong.Visible = false;
                pnlLoi.Visible = true;
            }

        }
    }
}